#version 460

in vec4 vertexColor;

flat in vec2 flatCorner;
in vec2 Pos1;
in vec2 Pos2;
in vec4 Coords;
in vec2 position;

uniform vec4 ColorModulator;

out vec4 fragColor;

vec4 colors[] = vec4[](//Ԥ��ɫ��
    vec4(0),
    vec4(218, 165, 32, 191) / 255,//����
    vec4(225, 168, 23, 0) / 255,//�Ľ�
    vec4(230, 192, 44, 191) / 255//����
);

int bitmap[][] = int[][](
    int[](1,5,1,1,1),
    int[](5,1,1,4,4),
    int[](1,1,4,4,4),
    int[](1,4,4,4,4),
    int[](1,4,4,4,4)
);


void main() {
    vec4 color = vertexColor;
    if (color.a == 0.0) {
        discard;
    }

    fragColor = color * ColorModulator;

    if (flatCorner != vec2(-1))
    {
        //Actual Pos ʵ��λ��
        vec2 APos1 = Pos1;
        vec2 APos2 = Pos2;
        APos1 = round(APos1 / (flatCorner.x == 0 ? 1 - Coords.z : 1 - Coords.w)); //Right-up corner
        APos2 = round(APos2 / (flatCorner.x == 0 ? Coords.w : Coords.z)); //Left-down corner

        ivec2 res = ivec2(abs(APos1 - APos2)) - 1; //Resolution of frame
        ivec2 stp = ivec2(min(APos1, APos2)); //Left-Up corner
        ivec2 pos = ivec2(floor(position)) - stp; //Position in frame

        vec4 col = vec4(0, 0, 0, 230) / 255.0;
         if ((pos.y + 16.0) <= pos.x)
        col = vec4(0, 0, 0, 150) / 255.0;
        col.rgb -= max(1 - length((pos - res / 8.0) / res) * 2, 0) / 10;

        ivec2 corner = min(pos, res - pos);

        if (corner.x < 5 && corner.y < 5)
        {
            int bit = bitmap[corner.y][corner.x];
            if (bit != 4)
                col = colors[bit];
        }
        else if (corner.x == 0)
            col = colors[1];
        else if (corner.y == 0)
            col = colors[3];

        fragColor = col;
         
    }
}